<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JLoader::register('AutogeneratorHtmlTable', AUTOGENERATOR_DIR_ROOT . '/lib/table.php');

class AutogeneratorViewTables extends JViewLegacy {

    protected $items;
    protected $pagination;
    protected $state;
    public $sContent;

    public function reviewCode() {
        $sFilePath = AUTOGENERATOR_DIR_CODE_TEMPLATE . 'tables' . DIRECTORY_SEPARATOR . 'TABLE_NAME.txt';

        $sContent = '';

        if (JFile::exists($sFilePath))
            $sContent = file_get_contents($sFilePath);
        else
            $sContent = 'error path';

        return $sContent;
    }

    public function display($tpl = null) {
        $oApp = JFactory::getApplication();

        AutogeneratorHelper::addSubmenu('tables');

        $this->items = $this->get('Items');
        $this->pagination = $this->get('Pagination');
        $this->state = $this->get('State');

        $this->addToolbar();

        $this->addFilter();


        parent::display($tpl);
    }

    protected function addToolbar() {
        JToolbarHelper::title(JText::_('COM_AUTOGENERATOR_TABLES_TITLE'));
        
        $oApp = JFactory::getApplication();
        if ($oApp->input->get('layout') == 'modal') {
            $this->sContent = AutogeneratorHelper::getTableContent($oApp->input->getInt('iTableId'));
        } else {
            JToolbarHelper::addNew('table.add');
            JToolbarHelper::editList('table.edit');
            JToolbarHelper::deleteList(JText::_('COM_AUTOGENERATOR_MESSAGE_CONFIRM_DELETE'), 'tables.delete', 'JTOOLBAR_DELETE');
            AutogeneratorHelper::addButtonCustom(JText::_('COM_AUTOGENERATOR_TABLES_CONFIRM_IMPORT_FIELDS_FOR_MULTI_TABLES'),'table.importForMultiTables', 'upload', 'upload', JText::_('COM_AUTOGENERATOR_IMPORT_TABLE_BUTTON_LABEL'), true);
        }
    }

    protected function addFilter() {
        JHtmlSidebar::setAction('index.php?option=com_autogenerator&view=tables');

        // Add filter.
        JHtmlSidebar::addFilter(
                JText::_('COM_AUTOGENERATOR_TABLES_SELECT_COMPONENT'), 'filter_component_id', JHtml::_('select.options', AutogeneratorHtmlTable::options(), 'value', 'text', $this->state->get('filter.component_id'))
        );

        $this->sidebar = JHtmlSidebar::render();
    }

    protected function getSortFields() {
        return array(
            'at.id' => JText::_('JGRID_HEADING_ID')
        );
    }

}
